#
#  This library provides a function to comfortable use bc (basic calculator, aka bench calculator)
#  (http://www.gnu.org/software/bc/manual/html_mono/bc.html) in a shell script.
#  It is made to use with libinclude.sh.
#  Because bc can't handle exponential format, it comes along with a function to convert from
#  exp to float format.
#  
#  Synopsis:
#     bc_clc EQUATION [SCALE]
#     bc_e2d NUMBER
#
# depends on bc

# default number of decimal places of the result
SCALE=4

clc () {
	local scale result
	
	if [ $2 ]; then
		scale="scale=$2;"
	elif [ $SCALE ]; then
		scale="scale=$SCALE;"
	fi
		
	result=`echo "$scale $1" | bc`
	if [ "$(echo $result | cut -c1)" = '.' ]; then
		result="0${result}"
	fi
	echo $result
}

e2d () {
	echo "$1" | \
      sed "s/E/e/g" | \
      awk -F"e" 'BEGIN{OFMT="%10.12f"} {print $1 * (10 ^ $2)}'
}

